using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Linq;

namespace SharpPopulatorLib
{

	public class ConfiguredFieldCollection
    {

        #region Atributes

        private List<ConfiguredField> _ConfiguredFieldCollection;

        #endregion

        #region Properties

        [Category(""), Description("")]
		public ConfiguredField this[String Name]
		{
			get
			{
                try
                { return _ConfiguredFieldCollection[IndexOf(Name)]; }
                catch (IndexOutOfRangeException)
                { return null; }
                catch (ArgumentOutOfRangeException)
                { return null; }
            }
            set 
            { _ConfiguredFieldCollection[IndexOf(Name)] = value; }
		}

        [Category(""), Description("")]
        public ConfiguredField this[ConfiguredField Field]
        {
            get
            { return _ConfiguredFieldCollection[_ConfiguredFieldCollection.FindIndex(f => f.FieldName == Field.FieldName)]; }
            set
            { _ConfiguredFieldCollection[_ConfiguredFieldCollection.FindIndex(f => f.FieldName == Field.FieldName)] = value; }
        }

        [Category(""), Description("")]
        public List<ConfiguredField> ConfiguredFields 
        {
            get
            { return _ConfiguredFieldCollection; }
            set
            { _ConfiguredFieldCollection = value; }
        }

        [Category(""), Description("")]
        public Int32 Count
        {
            get { return _ConfiguredFieldCollection.Count; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// To create a new istance of the class
        /// </summary>
        public ConfiguredFieldCollection()
		{
            // To prepare the class
            _ConfiguredFieldCollection = new List<ConfiguredField>();
		}

        #endregion

        #region Private methods

        /// <summary>
        /// To obtain the index of an elemento in the collection
        /// </summary>
        /// <param name="FieldName">Field's name</param>
        /// <returns></returns>
        private Int32 IndexOf(String FieldName)
        {
            //
            Int32 _i = -1;
            Boolean _Finded = false;

            foreach (ConfiguredField _ConfiguredField in _ConfiguredFieldCollection)
            {
                if (_ConfiguredField.FieldName == FieldName)
                { _Finded = true; }
                else
                { _i++; }

                if (_Finded)
                { return _i + 1; }
            }
            return _i;
        }

        #endregion

        #region Public methods

        /// <summary>
        /// To add a new pre-configured field
        /// </summary>
        /// <param name="FieldName">Name of field</param>
        /// <param name="Configuration">An instance of a class that represents it's configuration</param>
        /// <param name="Type">Enumaratio's ConfigurationPropertiesType value</param>
        public void Add(String FieldName, Object Configuration, ConfigurationPropertiesType Type, Boolean IsPrimaryKey)
		{
            //To add a new pre-configured field            
            this.Add(new ConfiguredField(FieldName, Configuration, Type, IsPrimaryKey));
		}

        /// <summary>
        /// To add a new pre-configured field
        /// </summary>
        /// <param name="NewConfiguredField">An valid instance of the class ConfiguredField</param>
        public void Add(ConfiguredField NewConfiguredField)
        {
            //To add an instance of the ConfiguredField to the collection
            if (_ConfiguredFieldCollection.Contains(NewConfiguredField))
            { }
            else
            { _ConfiguredFieldCollection.Add(NewConfiguredField); }
        }

        /// <summary>
        /// To clear the collection
        /// </summary>
        public void Clear()
        {
            //To clear the collection
            _ConfiguredFieldCollection.Clear();
        }

        /// <summary>
        /// To check if the ConfiguredField exist in the collection
        /// </summary>
        /// <returns></returns>
        public Boolean Exist(String FieldName)
        {
            //

            Int32 _Count = (from _item in _ConfiguredFieldCollection
                            where _item.FieldName == FieldName
                            select _item).Count();

            if (_Count == 0)
            { return false; }
            else
            { return true; }
        }

        #endregion
		
	}

}
